<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户名">
          <a-input placeholder="请输入用户名" v-decorator="['username', validatorRules.username ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手机号">
          <a-input placeholder="请输入手机号" v-decorator="['mobile', validatorRules.mobile ]" />
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="性别">
          <j-dict-select-tag  v-decorator="['sex', {}]" placeholder="请选择性别" :triggerChange="true" dictCode="sex"/>
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="性别">
          <a-radio-group name="sex" v-decorator="[ 'sex',{initialValue: 0 }]"  >
            <a-radio :value="0">男</a-radio>
            <a-radio :value="1">女</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="密码">
          <a-input placeholder="请输入密码" v-decorator="['password',  validatorRules.password]" />
        </a-form-item>
      

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="分成类型">
          <a-radio-group v-decorator="['promotionType', validatorRules.promotionType]" @change="handlerpromotionType">
            <a-radio-button value="1">按比例分成</a-radio-button>
            <a-radio-button value="2">按次数分成</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="请输入店铺分成比例"
          v-show="requiredpromotionNum"
        >
          <a-input placeholder="请输入店铺分成比例" v-decorator="['promotionNum', validatorRules.promotionNum]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="店铺订单每次分成数"
          v-show="!requiredpromotionNum"
        >
          <a-input placeholder="请输入店铺订单每次分成数" v-decorator="['promotionNum', validatorRules.promotionNum2]" />
        </a-form-item>


      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="昵称">
          <a-input placeholder="请输入昵称" v-decorator="['nickName', validatorRules.nickName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="真实姓名">
          <a-input placeholder="请输入真实姓名" v-decorator="['realName', validatorRules.realName]" />
        </a-form-item>
        <!-- <a-form-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl" :src="getAvatarView()" alt="头像" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item> -->
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="头像地址">-->
<!--          <a-input placeholder="请输入头像地址" v-decorator="['imageUrl', {}]" />-->
<!--        </a-form-item>-->
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import {validatePhone,validateIdNo} from '@/utils/validator'

  export default {
    name: "PromotersModalModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        requiredpromotionNum: true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
        promotionType: {
          rules: [
            { required: true, message: '请输入选择分成类型' },
            { max: 30, message: '商铺名称最多支持30个字符' }
          ],
          initialValue: '1'
        },
        promotionNum: {
          rules: [
            { required: this.requiredpromotionNum, message: '请输入分成比例' },
            { max: 30, message: '商铺名称最多支持30个字符' }
          ],
          initialValue: '0'
        },
        promotionNum2: {
          rules: [
            { required: !this.requiredpromotionNum, message: '请输入分成比例' },
            { max: 30, message: '商铺名称最多支持30个字符' }
          ],
          initialValue: '0'
        },
        username:{rules: [{ required: true, message: '请输入用户名!' }]},
        mobile:{rules: [{ required: true, message: '请输入手机号!' },{validator:validatePhone,trigger:blur}]},
        password:{rules: [{ required: true, message: '请输入密码' }]},
        // idCard: { rules: [{ required: true, message: '请输入身份证号' },{validator:validateIdNo,trigger:blur}] },
        nickName:{rules: [{ required: true, message: '请输入昵称' }]},
        realName:{rules: [{ required: true, message: '请输入真实姓名' }]},
        },
        url: {
          add: "/promoters/web/add",
          edit: "/promoters/web/edit",
        },
      }
    },
    created () {
    },
    methods: {
       handlerpromotionType(value) {
      this.requiredpromotionNum = !this.requiredpromotionNum
    },
      add () {
        // this.edit({});
         this.visible = true
         this.form.resetFields()
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.model.promotionType = this.model.promotionType == null ? '' : this.model.promotionType.toString()
          this.model.promotionNum = this.model.promotionNum == null ? '' : this.model.promotionNum.toString()
          this.form.setFieldsValue(
            pick(
              this.model,
              'username',
              'mobile',
              'sex',
              'wexinOpenId',
              'password',
              'type',
              'idCard',
              'state',
              'isDelete',
              'nickName',
              'realName',
              'salt',
              'imageUrl',
              'promotionType',
              'promotionNum'
             ))
		  //时间格式化
        })

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>